const connection = require("../app/database") 

class ProjectService {
  // 获取项目列表
  async getProjectListData() {
    const statement = "SELECT * FROM project;"
    const [result] = await connection.execute(statement)
    return result
  }
  // 获取单个项目信息
  async getProjectData(projectId) {
    const statement = "SELECT * FROM project WHERE id = ?;"
    const [result] = await connection.execute(statement, [projectId])
    return result[0]
  }
  // 根据id删除项目
  async removeProjectData(projectId) {
    const statement = "DELETE FROM project WHERE id = ?;"
    const [result] = await connection.execute(statement, [projectId])
    return result.affectedRows === 1 ? { msg: "删除成功" } : { msg: "删除失败" }
  }
  // 创建项目
  async createProjectData(name, sourceUrl, onlineUrl, introduce) {
    const statement = "INSERT INTO project (name, source_url, online_url, introduce) VALUES (?, ?, ?, ?);"
    const [result] = await connection.execute(statement, [name, sourceUrl, onlineUrl, introduce])
    return result.affectedRows === 1 ? { msg: "创建成功" } : { msg: "创建失败" }
  }
  // 更新项目
  async updateProjectData(projectId, { name, sourceUrl, onlineUrl, introduce }) {
    const statement = `
      UPDATE project
        SET name = ?, source_url = ?, online_url = ?, introduce = ?
      WHERE id = ?;
      `
    const [result] = await connection.execute(statement, [name, sourceUrl, onlineUrl, introduce, projectId])
    return result.affectedRows === 1 ? { msg: "更新成功" } : { msg: "更新失败" }
  }
}

module.exports = new ProjectService()